package cn.sfrog.project.biz.admin.controller;

import cn.sfrog.common.annotation.Log;
import cn.sfrog.common.annotation.RepeatSubmit;
import cn.sfrog.common.core.controller.BaseController;
import cn.sfrog.common.core.domain.AjaxResult;
import cn.sfrog.common.core.domain.entity.SysDictData;
import cn.sfrog.common.core.page.TableDataInfo;
import cn.sfrog.common.enums.BusinessType;
import cn.sfrog.common.enums.UserTypeEnum;
import cn.sfrog.common.utils.StringUtils;
import cn.sfrog.common.utils.poi.ExcelUtil;
import cn.sfrog.project.biz.admin.domain.vo.BizUserProfessionRecordFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizUserProfessionRecordQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizUserProfessionRecordVO;
import cn.sfrog.project.biz.admin.service.IBizUserProfessionRecordService;
import cn.sfrog.system.domain.vo.BizAuditVO;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import cn.sfrog.system.mapper.SysDictDataMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "管理后台-用户职业资质认证")
@RestController
@RequestMapping("/biz/professionRecord")
public class BizUserProfessionRecordController extends BaseController {
    @Resource
    private IBizUserProfessionRecordService bizUserProfessionRecordService;
    @Resource
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询用户职业资质认证列表
     */
    @ApiOperation("查询列表")
    @PreAuthorize("@ss.hasPermi('biz:professionRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizUserProfessionRecordQueryVO query) {
        if(StringUtils.isNotBlank(query.getProfessionName())){
            SysDictData dictData = sysDictDataMapper.selectTypeByDictLabel(query.getProfessionName());
            if(StringUtils.isNotNull(dictData) && StringUtils.isNotBlank(dictData.getDictType())){
                if("escort_level".equals(dictData.getDictType())){
                    //陪诊
                    query.setUserType(UserTypeEnum.ESCORT.getCode());
                }else if("biz_care_worker_level".equals(dictData.getDictType())){
                    //护理
                    query.setUserType(UserTypeEnum.CARE_WORKER.getCode());

                }else if("biz_doctor_title".equals(dictData.getDictType())){
                    //医生
                    query.setUserType(UserTypeEnum.DOCTOR.getCode());
                }
                query.setProfessionName(dictData.getDictValue());
            }else {
                query.setUserType("0");
            }
        }
        startPage();
        List<BizUserProfessionRecordVO> list = bizUserProfessionRecordService.selectBizUserProfessionRecordList(query);
        return getDataTable(list);
    }


    /**
     * 导出用户职业资质认证列表
     */
    @ApiOperation("导出")
    @PreAuthorize("@ss.hasPermi('biz:professionRecord:export')")
    @Log(title = "用户职业资质认证", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizUserProfessionRecordQueryVO query) {
        List<BizUserProfessionRecordVO> list = bizUserProfessionRecordService.selectBizUserProfessionRecordList(query);
        ExcelUtil<BizUserProfessionRecordVO> util = new ExcelUtil<BizUserProfessionRecordVO>(BizUserProfessionRecordVO.class);
        util.exportExcel(response, list, "用户职业资质认证数据");
    }

    /**
     * 获取用户职业资质认证详细信息
     */
    @ApiOperation("获取详情")
    @PreAuthorize("@ss.hasPermi('biz:professionRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bizUserProfessionRecordService.selectBizUserProfessionRecordById(id));
    }

    /**
     * 新增用户职业资质认证
     */
    @ApiOperation("新增")
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('biz:professionRecord:add')")
    @Log(title = "用户职业资质认证", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizUserProfessionRecordFormVO form) {
        return toAjax(bizUserProfessionRecordService.insertBizUserProfessionRecord(form));
    }

    /**
     * 修改用户职业资质认证
     */
    @ApiOperation("修改")
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('biz:professionRecord:edit')")
    @Log(title = "用户职业资质认证", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizUserProfessionRecordFormVO form) {
        return toAjax(bizUserProfessionRecordService.updateBizUserProfessionRecord(form));
    }

    /**
     * 删除用户职业资质认证
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('biz:professionRecord:remove')")
    @Log(title = "用户职业资质认证", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bizUserProfessionRecordService.deleteBizUserProfessionRecordByIds(ids));
    }

    /**
     * 更改状态用户职业资质认证
     */
    @ApiOperation("更改状态")
    @PreAuthorize("@ss.hasPermi('biz:professionRecord:changeStatus')")
    @Log(title = "用户职业资质认证", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody @Valid BizStatusChangeVO vo) {
        return AjaxResult.success(bizUserProfessionRecordService.changeStatus(vo));
    }

    /**
     * 审核
     * @param auditVO
     * @return
     */
    @ApiOperation("审核")
    @PreAuthorize("@ss.hasPermi('biz:professionRecord:audit')")
    @Log(title = "用户职业资质认证", businessType = BusinessType.UPDATE)
    @PostMapping("/audit")
    public AjaxResult audit(@RequestBody @Validated BizAuditVO auditVO){
        return AjaxResult.success(bizUserProfessionRecordService.audit(auditVO));
    }


}
